package per.example.arpproject.utils;


import lombok.Builder;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;

/**
 * @auther yaozhang Li
 * @ TO DO 文件工具类
 * @creatTime 2022/7/20 9:45
 **/
@Slf4j
public class FileUtil {


    /**
     * MultipartFile 转 File
     * @param multipartFile
     * @return
     */
    public static File multipartFile2File(MultipartFile multipartFile) {
        if (null == multipartFile || multipartFile.isEmpty()) {
            return null;
        }

        //文件上传前的名称
        String fileName = multipartFile.getOriginalFilename();
        File file = new File(fileName);
        return file;
    }


    public static FileInfo buildFileInfoPath(String filename, String fileSuffix, InputStream fileContent) {
        String tempFilename = filename.replaceAll("[\\\\/:*?\"<>|]", "");
        String outPutDir = System.getProperty("user.dir") + File.separator + "src" + File.separator + "file";
        //String uploadDir = FileTool.getUploadFileDir();
        String uploadDir = "";
        String filePath = outPutDir + uploadDir + File.separator + tempFilename+ ".pdf";
        return FileInfo.builder()
                .fileSuffix(fileSuffix)
                .pdfPath(filePath)
                .filename(tempFilename)
                .fileContent(fileContent)
                .build();
    }

    public static String getExtsionName(MultipartFile file) {
        return file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".")+1);
    }

    public static String getName(MultipartFile file) {
        return file.getOriginalFilename().substring(0,file.getOriginalFilename().lastIndexOf(".")+1);
    }

    @Data
    @Builder
    public static class FileInfo {
        private String pdfPath;
        private String fileSuffix;
        private String filename;
        private InputStream fileContent;
    }

/*    public static void createDocFileAndConvertToPdf(final FileInfo fileInfo) {
        OutputStream fos = null;
        File wordFile = null;
        try {
            wordFile = File.createTempFile(fileInfo.getFilename(), ".doc");
            fos = new FileOutputStream(wordFile);
            IOUtils.copy(fileInfo.getFileContent(), fos);
            //CreatePdfUtil.wordToPDF(wordFile.getAbsolutePath(), fileInfo.getPdfPath());
            AsposeLicenseUtil.convert2PdfPath(wordFile.getAbsolutePath(), fileInfo.getPdfPath());

        } catch (IOException e) {
            log.error("createDocFileAndConvertToPdf() 方法出错，filename:[{}]",fileInfo.getFilename(), e);
            throw new RuntimeException("创建 Office 文件异常或转换 PDF 文件异常");
        } finally {
            if (null != wordFile) {
                wordFile.delete();
            }

            IOUtils.closeQuietly(fos);
        }
    }*/

    /**
     * 复制一个目录及其子目录、文件到另外一个目录
     * @param src
     * @param dest
     * @throws IOException
     */
    public static void copyFolder(File src, File dest) throws IOException {
        if (src.isDirectory()) {
            if (!dest.exists()) {
                dest.mkdir();
            }
            String files[] = src.list();
            for (String file : files) {
                File srcFile = new File(src, file);
                File destFile = new File(dest, file);
                // 递归复制
                copyFolder(srcFile, destFile);
            }
        } else {
            InputStream in = new FileInputStream(src);
            OutputStream out = new FileOutputStream(dest);

            byte[] buffer = new byte[1024];

            int length;

            while ((length = in.read(buffer)) > 0) {
                out.write(buffer, 0, length);
            }
            in.close();
            out.close();
        }
    }

    /**
     * 删除path下所有文件包括文件夹
     * @param path
     * @param isIncludeRoot 是否要删除path(如果是文件夹)
     * @return true删除成功
     */
    public static boolean deleteAllFile(String path, boolean isIncludeRoot) {
        if(StringUtils.isBlank(path)) {
            return false;
        }

        File file = new File(path);
        if (!file.exists()) {
            return false;
        }

        if(file.isFile()) {
            return file.delete();
        }

        File[] fileList = file.listFiles();
        boolean res = true;
        for (File f : fileList) {
            if(f.isFile()) {
                res = res && f.delete();
            } else if(f.isDirectory()) {
                res = res && deleteAllFile(f.getAbsolutePath(), true);
            }
        }

        if(isIncludeRoot) {
            res = res && file.delete();
        }

        return res;
    }

}
